package com.lanhai.zhonglai.dispatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.AgvCurrentTransEnum;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IControlArea;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IDotEvent;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.third.StationFactory;

/**
 * ALD干法专用 //弃用
 *
 * @author zhengwei
 */
@Component
public class JudgementALD_G2 {

    @Autowired
    IStation iStation;

    @Autowired
    IStationMount iStationMount;

    @Autowired
    IAgv iAgv;

    @Autowired
    IDot iDot;

    @Autowired
    ISendData sendTools;

    @Autowired
    IAgvEvent iAgvEvent;

    @Autowired
    IDotEvent iDotEvent;

    @Autowired
    IRoad iRoad;

    @Autowired
    ICmdLog iCmdLog;

    @Autowired
    IControlArea iControlArea;

    @Autowired
    AgvCmdMap agvCmdMap;

    @Autowired
    StationLeaveCheck stationCheck;

    /**
     * 超过10个花篮的
     *
     * @param agv
     * @param station
     * @param dot
     * @param agvMount
     * @param dmount
     */
    public void judgement_twice(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount, StationMountBean dmount) {
        IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());

        if (StationCmdMap.canLeave(station.getDotno()) && agv.isFinishTrans() == false) {
            if (agvMount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) {
                byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
                sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
                        station.getStationname());


                if (iControlArea.isLockedForDot(dot.getDotno(), agv.getAgvId())) {
                    //前方交管区被管了
                    return;
                }
                stationCheck.check(dot, agv, station, stationEvent, agvMount);
                return;
            } else {
                //其它状态，一般来说，需要AGV出料
                //用于最后一段的“假离开”
                StationCmdMap.clearState(dot.getDotno());

                //更新AGV
                agv.setFakeLeave(true);
                iAgv.saveCache(agv);

                //给机台发送AGV已经离开 ,直到对方不发“允许离开”
                byte[] goout = stationEvent.agvLeave(station.getLine(), station.getFillType());
                sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV假离开", goout,
                        station.getStationname());
                return;

            }
        }
        // ********************* 处理转动 **********************
        if (StationCmdMap.canReceive(station.getDotno()) && agv.getCurrentTrans() != AgvCurrentTransEnum.None) {
            // 传输过程中，直接过滤
            if (agv.isTransing() == false) {
                agv.setTransing(true);
                iAgv.saveCache(agv);
                switch (agv.getCurrentTrans()) {
                    //只有出料的动作
                    case DownIn:
                        iAgvEvent.transDown(agv, true);
                        break;
                    case DownOut:
                        iAgvEvent.transDown(agv, false);
                        break;
                    case UpIn:
                        iAgvEvent.transUp(agv, true);
                        break;
                    case UpOut:
                        iAgvEvent.transUp(agv, false);
                        break;
                    case UpInAndDownOut:
                        iAgvEvent.transUpInAndDownOut(agv);
                        break;
                    case UpOutAndDownIn:
                        iAgvEvent.transUpOutAndDownIn(agv);
                        break;
                    default:
                        MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
                        break;
                }
            }
            return;
        }
        // ****************  处理传动 end  ****************


        // **************** 处理传输状态  ********
        if (agvMount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull && agv.isFinishTrans() == false) {
            //满足离开的装料状态
            agv.setFinishTrans(true);
            iAgv.saveCache(agv);
        }

        if (agv.isFinishTrans()) {
            //申请离开
            if (agv.getLastSaveDot() != dot.getDotno()) {
                iCmdLog.saveTransLog(agv.getAgvId(), dot.getDotno(), station, TransDirectionEnum.Both);
                agv.setLastSaveDot(dot.getDotno());
                iAgv.saveCache(agv);
            }

            byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType());
            sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
                    station.getStationname());
            return;
        }

        // **************** 处理传输状态 end ********

        // 机台允许传输，开始申请皮带转动（申请传输）
        if (StationCmdMap.canTrans(station.getDotno())) {

            if (agvMount.getMountType() == AgvMountTypeEnum.AllFull) {
                //全满，AGV上出
                if (dmount.isAllowIn()) {
                    agv.setCurrentTrans(AgvCurrentTransEnum.UpOut);
                    agv.setBothTrans(false);
                    agv.setApplyTransType(TransDirectionEnum.OnlyIn);
                    iAgv.saveCache(agv);
                    MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入");
                    byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
                    sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
                            station.getStationname());
                } else {
                    //料不够申请
                    MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(), dmount.getOutNum()));

                }
                return;
            } else if (agvMount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty) {
                //上满下空，因为本类是处理超过10个的，这种情况只能下层收货
                if (dmount.isAllowOut()) {
                    //AGV下层收货
                    agv.setCurrentTrans(AgvCurrentTransEnum.DownIn);
                    agv.setBothTrans(false);
                    agv.setApplyTransType(TransDirectionEnum.OnlyOut);
                    iAgv.saveCache(agv);
                    MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入");
                    byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
                    sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
                            station.getStationname());
                } else {
                    //料不够申请
                    MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(), dmount.getOutNum()));

                }
                return;
            } else {
                //其它状态

                return;
            }

        }

        // ********  机台允许传输  end ********
        if (agvMount.getMountType() == AgvMountTypeEnum.AllFull) {
            //先发AGV离开，再发AGV就位
            if (dmount.isAllowIn()) {
                byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
                sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
                        station.getStationname());
            } else {
                if (agv.isFakeLeave()) {
                    //假离开了，前面已经发过，此处不发
                } else {
                    //申请假离开，申请几次就可以了
                    byte[] cmd = stationEvent.applyLeave(station.getLine(), TransDirectionEnum.Both);
                    sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", cmd,
                            station.getStationname());
                }

            }

        } else {
            //其它状态直接发就位

            byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
            sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
                    station.getStationname());

        }

    }
}
